/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ssau.berlin.ejb.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import ru.ssau.berlin.entity.Person;
import ru.ssau.berlin.entity.PersonAttribute;
import ru.ssau.berlin.entity.PersonGroup;
import ru.ssau.berlin.entity.PersonParameter;
import ru.ssau.berlin.entity.PersonRole;
import ru.ssau.berlin.entity.PersonType;

/**
 *
 * @author Caesar
 */
@Stateless
public class PersonFacade implements PersonFacadeLocal {
    
    @PersistenceContext
    private EntityManager em;

    public void create(Person person) {
        em.persist(person);
    }

    public void edit(Person person) {
        for (PersonParameter parameter: person.getParameters()) {
            em.merge(parameter);
        }
        for (PersonRole role: person.getRoles()) {
            em.merge(role);
        }
        em.merge(person);
    }

    public void remove(Person person) {
        Person temp = em.merge(person);
        for (PersonParameter parameter: temp.getParameters()) {
            em.remove(parameter);
        }
        em.remove(temp);
    }

    public Person find(long id) {
        return em.find(Person.class, id);
    }

    public List<Person> findAll() {
        return em.createNamedQuery("findAllPersons").getResultList();
    }

    public List<Person> findByName(String name) {
        return em.createNamedQuery("findPersonByName")
                .setParameter("personName", name).getResultList();
    }

    public PersonType findPersonType(String name) {
        return (PersonType) em.createNamedQuery("findPersonTypeByName")
                .setParameter("typeName", name).getSingleResult();
    }

    public PersonType findPersonType(long id) {
        return em.find(PersonType.class, id);
    }

    public List<PersonType> findPersonTypes() {
        return em.createNamedQuery("findPersonTypes").getResultList();
    }

    public List<Person> findNonAchievers(PersonGroup faculty) {
        return em.createNamedQuery("findNonAchievers")
                .setParameter("faculty", faculty).getResultList();
    }

    public List<Person> findInstructors() {
        return em.createNamedQuery("findInstructors").getResultList();
    }

    public List<Person> findSalaries(PersonGroup group) {
        return em.createNamedQuery("findSalaries")
                .setParameter("department", group).getResultList();
    }

    public List<Person> findStudents() {
        return em.createNamedQuery("findStudents").getResultList();
    }

    public void createType(PersonType type) {
        em.persist(type);
    }

    public void editType(PersonType type) {
        em.merge(type);
    }

    public void removeType(PersonType type) {
        em.remove(em.merge(type));
    }

    public void createAttribute(PersonAttribute attr) {
        em.persist(attr);
    }

    public void editAttribute(PersonAttribute attr) {
        em.merge(attr);
    }

    public void removeAttribute(PersonAttribute attr) {
        em.remove(em.merge(attr));
    }

    public List<PersonAttribute> findPersonAttributes() {
        List<PersonAttribute> result = em.createNamedQuery("findPersonAttributes").getResultList();
        // for lazy list evaluation in the current session
        // for (PersonAttribute attr : result) {
        //    attr.getPersonTypes().size();
        //}
        return result;
    }

    public PersonAttribute findPersonAttribute(long id) {
        return (PersonAttribute) em.find(PersonAttribute.class, id);
    }

    public PersonAttribute findPersonAttribute(String name) {
        return (PersonAttribute) em.createNamedQuery("findPersonAttributeByName")
                .setParameter("attrName", name).getSingleResult();
    }

    public void createParameter(PersonParameter parameter) {
        em.persist(parameter);
    }

    public PersonParameter findParameter(long id) {
        return em.find(PersonParameter.class, id);
    }

    public void removeParameter(PersonParameter parameter) {
        parameter = findParameter(parameter.getId());
        em.remove(parameter);
    }

}
